package com.robusta.cityuate.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.robusta.cityuate.base.CityuateActivity;
import com.robusta.cityuate.objects.Spot;

public class QRActivity extends CityuateActivity {

	static String contents;
	public static final String SCAN_MODE="scanMode";
	public enum ScanMode{SPOT_DETAILS,CHECK_IN,CHECK_OUT};
	public ScanMode scanMode;

	public static final int REQUEST_CODE = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		scanMode=(ScanMode)getIntent().getExtras().getSerializable(SCAN_MODE);
		Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		startActivityForResult(intent, 0);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				contents = intent.getStringExtra("SCAN_RESULT");
				// String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				Log.i("Barcode Result", contents);

				Spot spot = new Spot();
				spot.setId(Long.parseLong(contents));
				Intent i1 ;
				switch (scanMode) {
				case SPOT_DETAILS:
					i1=new Intent(this, SpotProfileActivity.class);
					i1.putExtra(Spot.SPOT, spot);
					startActivity(i1);
					finish();
					break;

				case CHECK_IN:
//					Intent i1 = new Intent(this, SpotProfileActivity.class);
//					i1.putExtra(Spot.SPOT, spot);
//					startActivity(i1);
//					finish();
					break;
				case CHECK_OUT:
//					Intent i1 = new Intent(this, SpotProfileActivity.class);
//					i1.putExtra(Spot.SPOT, spot);
//					startActivity(i1);
//					finish();
					break;
				default:
					break;
				}
				

				// Handle successful scan
			} else if (resultCode == RESULT_CANCELED) {

				Log.i("Barcode Result", "Result canceled");
				finish();
			}
		}
	}

}
